import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    token:'',
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    updateToken(state, newtoke){
      state.token = newtoke
    },
    updataUserInfo(state, info){
      state.userInfo = info
    }
  },
  actions: {
    async initUserInfo(context){
      const {data: res} = await axios.get('/my/userinfo', {
        headers: {Authorization:context.state.token}
      })
      // console.log(res)
      if(res.code === 0) {
        context.commit('updataUserInfo', res.data)
      }
    }
  },
  modules: {
  }
})
